package com.ajk.server.controller;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ajk.server.exception.AjkException;
import com.ajk.server.exception.ErrorConstant;
import com.ajk.server.model.CheckItemDict;
import com.ajk.server.model.DepartmentDict;
import com.ajk.server.model.DictDiseaseKey;
import com.ajk.server.model.Disease;
import com.ajk.server.model.DiseaseCategoryDict;
import com.ajk.server.model.DiseaseKeyword;
import com.ajk.server.model.Symptom;
import com.ajk.server.model.query.CheckItemDictQuery;
import com.ajk.server.model.query.DiseaseKeywordQuery;
import com.ajk.server.model.query.DiseaseQuery;
import com.ajk.server.model.response.BodypartDictRes;
import com.ajk.server.model.response.DiseaseRes;
import com.ajk.server.model.response.ResultList;
import com.ajk.server.model.response.ResultObject;
import com.ajk.server.service.BodypartDictService;
import com.ajk.server.service.DepartmentDictService;
import com.ajk.server.service.DictDiseaseKeyService;
import com.ajk.server.service.DiseaseCategoryDictService;
import com.ajk.server.service.DiseaseKeywordService;
import com.ajk.server.service.DiseaseService;
import com.ajk.server.utils.IntegerUtil;

/**
 * 疾病相关接口
 * @author chenxuan
 * 
 * t_disease
 * dict_bodypart
 * r_disease_bodypart
 */
@Controller
@RequestMapping(value="/disease")
public class DiseaseController {
	
	Logger logger = LoggerFactory.getLogger(DiseaseController.class);

	@Autowired
	private DiseaseService diseaseService;
	
	@Autowired
	DiseaseCategoryDictService diseaseCategoryDictService;
	
	@Autowired
	private DictDiseaseKeyService dictDiseaseKeyService;
	
	@Autowired
	private DiseaseKeywordService diseaseKeywordService;
	
	@Autowired
	private BodypartDictService bodypartDictService;
	
	@Autowired
	private DepartmentDictService departmentDictService;
	/**
	 * 疾病分类列表
	 * @return
	 */
	@RequestMapping(value="/category/list",method=RequestMethod.GET)
	@ResponseBody
	public ResultList<DiseaseCategoryDict> getDiseaseCategoryDictList(){
		ResultList<DiseaseCategoryDict> list = new ResultList<DiseaseCategoryDict>();
		list.setItemList(diseaseCategoryDictService.findAll());
		return list;
	}
	
	/**
	 * 身体部位列表
	 * @return
	 */
	@RequestMapping(value="/bodypart/list",method=RequestMethod.GET)
	@ResponseBody
	public ResultList<BodypartDictRes> getBodypartDictList(){
		ResultList<BodypartDictRes> list = new ResultList<BodypartDictRes>();
		List<BodypartDictRes> result = bodypartDictService.findAll();
		//List<BodypartDictRes> result = bodypartDictService.findAllWithCache();
		list.setItemList(result);
		return list;
	}
	
	/**
	 * 根据身体部位ID获取症状列表
	 * @param bodypartId
	 * @return
	 */
	@RequestMapping(value="/bodypart/symptom/list/{bodypartId}",method=RequestMethod.GET)
	@ResponseBody
	public ResultList<Symptom> getsymptomListByBodypartId(@PathVariable("bodypartId") Integer bodypartId){
		if(null == bodypartId){
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(), "身体部位ID不能为空");
		}
		ResultList<Symptom> list = new ResultList<Symptom>();
		List<Symptom> result = diseaseService.findSymptonListByBodypartId(bodypartId);
		list.setItemList(result);
		return list;
	}
	
	@RequestMapping(value="/symptom/disease/list",method=RequestMethod.GET)
	@ResponseBody
	public ResultList<DiseaseRes> getDiseaseListForSymptomId(DiseaseQuery query){
		ResultList<DiseaseRes> resultList = new ResultList<DiseaseRes>();
		List<DiseaseRes> diseaseRes = diseaseService.findDiseaseListForSymptomId(query);
		resultList.setItemList(diseaseRes);
		resultList.setExt( bodypartDictService.findSymptomRelation(query.getSymptomId()));
		return resultList;
	}
	
	@RequestMapping(value="/detail/{diseaseId}",method=RequestMethod.GET)
	@ResponseBody
	public ResultObject<Disease> getDiseaseById(@PathVariable("diseaseId") Integer diseaseId){
		Disease disease = diseaseService.findDiseaseByIdWithCache(diseaseId);
		
		return new ResultObject<>(disease);
	}
	
	
	/**
	 * 疾病详情
	 * @return
	 */
	@RequestMapping(value="/detail",method=RequestMethod.GET)
	@ResponseBody
	public ResultList<DictDiseaseKey> getDiseaseCategoryDictList(DiseaseQuery query){
		ResultList<DictDiseaseKey> resultList =  new ResultList<DictDiseaseKey>();
		if(query.getId()== null || query.getId() == 0){
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"疾病ID不能为空");
		}
		List<DictDiseaseKey> list = dictDiseaseKeyService.queryDictDiseaseKey(query.getId());
		Disease dis = diseaseService.findDiseaseById(query.getId());
		DepartmentDict departmentDict = departmentDictService.findByChildId(dis.getDepartmentId());
		resultList.setItemList(list);
		
		if(departmentDict!=null){
			Disease disease = new Disease();
			disease.setDepartmentId(departmentDict.getId());
			resultList.setExt(disease);
		}
		
		return resultList;
	}
	
	/**
	 * 疾病热词列表
	 * @return
	 */
	@RequestMapping(value="/hotword/list",method=RequestMethod.GET)
	@ResponseBody
	public ResultList<DiseaseKeyword> getDiseaseHotwordList(DiseaseKeywordQuery query){
		ResultList<DiseaseKeyword> resultList =  new ResultList<DiseaseKeyword>();
		List<DiseaseKeyword> result = null;
		List<DiseaseKeyword> all = diseaseKeywordService.findDiseaseKeywordByQueryWithCache();
		if (CollectionUtils.isNotEmpty(all)) {
			Integer total = all.size();
			query.setCount(total);
			Integer start = query.getStart();
			Integer limit = query.getLimit();
			if(start!=null && IntegerUtil.isPositiveInteger(limit)){
				if(start>=0 && start<total){
					int fromIndex = start;
					int toIndex = start+limit;
					if((start+limit)>total){
						toIndex = total;
					}
					result = all.subList(fromIndex, toIndex);
				}
			}else{
				result = all;
			}
		}else{
			query.setCount(0);
		}
		resultList.setExt(query);
		resultList.setItemList(result);
		return resultList;
	}
	/**
	 * 疾病详情
	 * @return
	 */
	@RequestMapping(value="/search",method=RequestMethod.GET)
	@ResponseBody
	public ResultList<DiseaseRes> diseaseSearch(DiseaseQuery query){
//		if(null == keyword){
//			if(query.getKeyword() == null || query.getKeyword().trim().isEmpty()){
//				throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"搜索关键字不能为空");
//			}
//			
//		}else{
//			query.setKeyword(keyword);
//		}
		if(StringUtils.isEmpty(query.getKeyword())){
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"搜索关键字不能为空");
		}
		List<DiseaseRes> list = diseaseService.findDiseaseByQuery(query);
		return new ResultList<DiseaseRes>(list);
	}
	
	/**
	 * 疾病详情
	 * @return
	 */
	@RequestMapping(value="/manager/list",method=RequestMethod.GET)
	@ResponseBody
	public ResultList<DiseaseRes> diseaseManagerList(DiseaseQuery query){
//		if(null == keyword){
//			if(query.getKeyword() == null || query.getKeyword().trim().isEmpty()){
//				throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"搜索关键字不能为空");
//			}
//			
//		}else{
//			query.setKeyword(keyword);
//		}
		ResultList<DiseaseRes> resultList  = new ResultList<DiseaseRes>();
		List<DiseaseRes> itemList = diseaseService.managerList(query);
		resultList.setItemList(itemList);
		Integer rowCount = diseaseService.countDiseaseByQuery(query);
		resultList.setExt(rowCount);	
		return resultList;
	}
	
	
	
}